﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.Framework;

namespace com.subb.WebFramework
{
    public class FW_MemDefModualDAL
    {
        public List<FW_MemDefModual> GetAllDefModual(string memId)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.FW_MemDefModual.Where(c => c.MemberID.Equals(memId, StringComparison.CurrentCultureIgnoreCase))
                    .OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ModID);
                List<FW_MemDefModual> modList = query.ToList();
                return modList;
            }
        }

        public List<FW_MemDefModual> GetAllDefModual(string memId, int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.FW_MemDefModual.Where(c => c.MemberID.Equals(memId, StringComparison.CurrentCultureIgnoreCase))
                    .OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ModID);
                ttlCount = query.Count();
                List<FW_MemDefModual> modList = query.Skip(start).Take(limit).ToList();
                return modList;
            }
        }

        public List<MemDefModual> GetAllDefUIModual(string memId)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                List<MemDefModual> modList = new List<MemDefModual>();
                var query = from mod in ctx.FW_MemDefModual
                            join item in ctx.FW_MenuItem on mod.MenuItemID equals item.ItemID
                            where mod.MemberID.Equals(memId, StringComparison.CurrentCultureIgnoreCase)
                            select new 
                            { 
                                ModID = mod.ModID, 
                                MemberID = mod.MemberID, 
                                MenuItemID = mod.MenuItemID, 
                                SortNum = mod.SortNum, 
                                ModualName = mod.ModualName, 
                                ModualImg = mod.ModualImg, 
                                MenuItemName = item.ItemName, 
                                MenuItemUrl = item.ItemUrl 
                            };
                foreach (var c in query)
                {
                    MemDefModual modual = new MemDefModual
                    {
                        ModID = c.ModID,
                        MemberID = c.MemberID,
                        MenuItemID = c.MenuItemID,
                        MenuItemName = c.MenuItemName,
                        MenuItemUrl = c.MenuItemUrl,
                        SortNum = c.SortNum,
                        ModualName = c.ModualName,
                        ModualImg = c.ModualImg
                    };
                    modList.Add(modual);
                }
                return modList;
            }
        }

        public List<MemDefModual> GetAllDefUIModual(string memId, int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                List<MemDefModual> modList = new List<MemDefModual>();
                var query = from mod in ctx.FW_MemDefModual
                            join item in ctx.FW_MenuItem on mod.MenuItemID equals item.ItemID
                            where mod.MemberID.Equals(memId, StringComparison.CurrentCultureIgnoreCase)
                            select new
                            {
                                ModID = mod.ModID,
                                MemberID = mod.MemberID,
                                MenuItemID = mod.MenuItemID,
                                SortNum = mod.SortNum,
                                ModualName = mod.ModualName,
                                ModualImg = mod.ModualImg,
                                MenuItemName = item.ItemName,
                                MenuItemUrl = item.ItemUrl
                            };
                query = query.OrderByDescending(c => c.SortNum).ThenBy(c => c.ModID);
                ttlCount = query.Count();
                query = query.Skip(start).Take(limit);
                foreach (var c in query)
                {
                    MemDefModual modual = new MemDefModual
                    {
                        ModID = c.ModID,
                        MemberID = c.MemberID,
                        MenuItemID = c.MenuItemID,
                        MenuItemName = c.MenuItemName,
                        MenuItemUrl = c.MenuItemUrl,
                        SortNum = c.SortNum,
                        ModualName = c.ModualName,
                        ModualImg = c.ModualImg
                    };
                    modList.Add(modual);
                }
                return modList;
            }
        }

        public FW_MemDefModual GetModual(int mid)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                FW_MemDefModual modual = ctx.FW_MemDefModual.SingleOrDefault(c => c.ModID == mid);
                return modual;
            }
        }

        public int CreateModual(MemDefModual modual)
        {
            int mid = 0;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                FW_MemDefModual fmModual = new FW_MemDefModual
                {
                    MemberID = modual.MemberID,
                    MenuItemID = modual.MenuItemID,
                    SortNum = modual.SortNum ?? 0,
                    ModualName = modual.ModualName,
                    ModualImg = modual.ModualImg
                };
                ctx.FW_MemDefModual.AddObject(fmModual);
                ctx.SaveChanges();
            }
            return mid;
        }

        public bool UpdateModual(int modId, MemDefModual modual)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                FW_MemDefModual fmModual = ctx.FW_MemDefModual.FirstOrDefault(c => c.ModID == modId);
                if (fmModual != null)
                {
                    fmModual.MenuItemID = modual.MenuItemID;
                    fmModual.SortNum = modual.SortNum;
                    fmModual.ModualName = modual.ModualName;
                    fmModual.ModualImg = modual.ModualImg;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveModual(int modId)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                FW_MemDefModual fmModual = ctx.FW_MemDefModual.FirstOrDefault(c => c.ModID == modId);
                if (fmModual != null)
                {
                    ctx.FW_MemDefModual.DeleteObject(fmModual);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
